//
//  WCNMessageCel.m
//  WCN
//
//  Created by wsj2015 on 15/9/24.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNMessageCel.h"
#import "MyUtil.h"
#import "WCNDashBorder.h"
#import "Macros.h"
#import "WCImageView +setImage.h"
#import "NSString+Utilities.h"
#define kWidth self.contentView.bounds.size.width

@implementation WCNMessageCel
{
    WCNDashBorder *_partOneView; //
    WCNDashBorder *_partTwoView;
    UIImageView *_patientIconView;
    UILabel *_phoneLabel;
    UILabel *_messContentLabel;
    UILabel *_contentLabel;
   
    
    
    CGFloat _cellH;

}
- (void)awakeFromNib {
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
     
        //粉丝信息部分
        _partOneView =[[WCNDashBorder alloc]initWithFrame:CGRectMake(0, 0, kWidth, 60)];
         
        _partOneView.bottomBorder =YES;
        [self.contentView addSubview:_partOneView];
        
        _patientIconView =[[UIImageView alloc]init];
        _patientIconView.frame =CGRectMake(5, 9, 42, 42);
        [_partOneView addSubview:_patientIconView];
        
        CGFloat iconMax =CGRectGetMaxX(_patientIconView.frame);
        _phoneLabel =[[UILabel alloc]initWithFrame:CGRectMake(iconMax +5, 12, 180,20)];
        _phoneLabel.font =font(15);
        [_partOneView addSubview:_phoneLabel];
        
        //信息内容 可变的内容
        CGFloat phoneMaxY =CGRectGetMaxY(_phoneLabel.frame);
        _messContentLabel =[[UILabel alloc]initWithFrame:CGRectMake(iconMax +5, phoneMaxY +5, kWidth -iconMax -10, 18)];
        _messContentLabel.textColor =[UIColor orangeColor];
        _messContentLabel.font =font(13);
        _messContentLabel.numberOfLines =0;
        [_partOneView addSubview:_messContentLabel];
        
        
        
         //护士部分
        _partTwoView =[[WCNDashBorder alloc]init];
        _partTwoView.frame =CGRectMake(0, 64, kWidth, 64);
        _partTwoView.bottomBorder =YES;
        [self.contentView addSubview:_partTwoView];
        
         //回复内容
        _replyContentLabel =[[UILabel alloc]init];
        _replyContentLabel.frame =CGRectMake(30, 0, kWidth-50,40);
        _replyContentLabel.text =@"点击回复";
     
        _replyContentLabel.font =font(13);
        _replyContentLabel.numberOfLines =0;
        [_partTwoView addSubview:_replyContentLabel];
        
        CGFloat replyContentMaxY =CGRectGetMaxY(_replyContentLabel.frame);
        //回复时间
        _replyTimeLabel =[[UILabel alloc]init];
        _replyTimeLabel.text =@"等待回复";
        _replyTimeLabel.frame =CGRectMake(40, replyContentMaxY+4, kWidth -40, 18);
        _replyTimeLabel.font =font(13);

        _replyTimeLabel.textColor =HexRGB_WSJ(52, 104, 174);
        _replyTimeLabel.textAlignment =NSTextAlignmentRight;
        [_partTwoView addSubview:_replyTimeLabel];
        
        
    }
 
    return self;
}
- (void)configWithMessageModel:(WCNMessageContainer *)message
{
    
    [_patientIconView setImageWithUrl:message.avatarFileId];
    
    _phoneLabel.text =[message.nickName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    _messContentLabel.text =message.messageContent;
   
    if (message.replyContent) {
        _replyContentLabel.text =[NSString stringWithFormat:@"%@",message.replyContent];
    }else{
      
        _replyContentLabel.text =[NSString stringWithFormat:@"暂未回复"];
    }
   
    if (message.replyTime ) {
         NSString *timeStr =[NSString stringWithFormat:@"%@",message.replyTime];
        _replyTimeLabel.text = [NSString timeToString:[timeStr longLongValue]];
    }else{
       _replyTimeLabel.text = [NSString stringWithFormat:@"等待回复"];
    }
    
    [self autoJustFrames];
//
   
   
}
- (void)autoJustFrames
{
    //改变回复内容的高度
    CGRect messFrame =_messContentLabel.frame;
 
    CGFloat messLabelH =[_messContentLabel.text boundingRectWithSize:CGSizeMake(messFrame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font(13)} context:nil].size.height;
    
    messFrame.size.height = messLabelH;
    _messContentLabel.frame =messFrame;
    CGFloat messMaxY =CGRectGetMaxY(_messContentLabel.frame);
    
    //改变上部分view的高度
    CGRect partOneFrame =_partOneView.frame;
    if (messMaxY <=64) {
        partOneFrame.size.height =64;
    }else{
        partOneFrame.size.height =messMaxY +2;
    }
    
    _partOneView.frame =partOneFrame;
    
 
    CGRect partTwoFrame =_partTwoView.frame;
    CGRect replyContentFrame =_replyContentLabel.frame;
    CGRect replyTimeFrame =_replyTimeLabel.frame;
    CGFloat replyContentH =[_replyContentLabel.text boundingRectWithSize:CGSizeMake(kWidth -50, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font(13)} context:nil].size.height;
    if (replyContentH <=44) {
        replyContentH =44;
    }
    
    
    //调整回复内容高度
    replyContentFrame.size.height =replyContentH;
    NSLog(@"%@",_replyContentLabel.text);
    _replyContentLabel.frame =replyContentFrame;
    
    NSLog( @"%@",NSStringFromCGRect(_replyContentLabel.frame));
    //调整 时间y坐标
    CGFloat replyContentMaxY =CGRectGetMaxY(_replyContentLabel.frame);
    replyTimeFrame.origin.y =replyContentMaxY +2;
    _replyTimeLabel.frame =replyTimeFrame;
    
    CGFloat replyTimeMaxY =CGRectGetMaxY(_replyTimeLabel.frame);
    //调整下面背景视图的高度 和y 坐标
    
    partTwoFrame.origin.y =CGRectGetMaxY(_partOneView.frame)+2;
    
    partTwoFrame.size.height = replyTimeMaxY +2;
    _partTwoView.frame =partTwoFrame;
    
    _cellH =CGRectGetMaxY(_partTwoView.frame);



}
- (void)layoutSubviews
{
    [super layoutSubviews];
   
}
- (CGFloat)heightForCell
{
    return _cellH;
 
}
+ (WCNMessageCel *)CellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexpath
{
     static NSString *cellId =@"messageCellId";
    WCNMessageCel *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell =[[WCNMessageCel alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.backgroundColor =[UIColor whiteColor];
    return cell;
 
}
@end
